package cn.yunyichina.reservation.controller.business;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.constant.Interface;
import cn.yunyichina.reservation.util.DateUtil;
import cn.yunyichina.reservation.util.RequestUtil;
import cn.yunyichina.reservation.vo.OrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Lullaby on 2016/8/29.
 */
@RestController
@RequestMapping("order")
public class OrderController {

    private static final String RESERVATION_ORDER_GENERATE = "yyt.reservation.order.generate";
    private static final String RESERVATION_ORDER_LIST = "yyt.reservation.order.list";
    private static final String RESERVATION_ORDER_DETAIL = "yyt.reservation.order.detail";
    private static final String RESERVATION_ORDER_CANCEL = "yyt.reservation.order.cancel";

    /**
     * 订单确认
     * @param request
     * @return
     */
    @PostMapping("confirm")
    public HttpResponse confirmOrder(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = RequestUtil.getAllParameters(request);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_ORDER_GENERATE, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.RPC_NOT_FOUND.getCode(), ResponseEnum.RPC_NOT_FOUND.getMessage());
        } else if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.success("订单确认成功");
            httpResponse.setData(Jackson.getBeanJsonData(serviceResponse.getResult(), OrderVo.class));
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), "订单确认失败");
        }
        return httpResponse;
    }

    @GetMapping("list")
    public HttpResponse listOrder(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = RequestUtil.getAllParameters(request);
        if (StringUtils.isBlank(params.get("user_id"))) {
            return httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getCode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        String range = params.get("order_date_range");
        if (StringUtils.isNotBlank(range)) {
            String beginCreateDate = null;
            String endCreateDate = DateUtil.getIntervalDateString(-1);
            switch (Byte.valueOf(range)) {
                case 1:
                    beginCreateDate = DateUtil.getIntervalDateString(0);
                    break;
                case 2:
                    beginCreateDate = DateUtil.getIntervalDateString(3);
                    break;
                case 3:
                    beginCreateDate = DateUtil.getIntervalDateString(7);
                    break;
                case 4:
                    beginCreateDate = DateUtil.getIntervalDateString(30);
                    break;
                default:
                    break;
            }
            if (StringUtils.isNotBlank(beginCreateDate)) {
                params.put("begin_create_time", beginCreateDate);
                params.put("end_create_time", endCreateDate);
            }
        }
        Response serviceResponse = ReservationService.callUnderlineFromInternal(
                RESERVATION_ORDER_LIST, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null)
            return httpResponse.failure(ResponseEnum.RPC_NOT_FOUND.getCode(), ResponseEnum.RPC_NOT_FOUND.getMessage());
        else if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.setData(Jackson.getBeanJsonData(serviceResponse.getResult(), Map.class));
        } else {
            httpResponse.failure(ResponseEnum.RPC_EXCEPTION.getCode(), ResponseEnum.RPC_EXCEPTION.getMessage());
        }
        return httpResponse;
    }

    @GetMapping("detail")
    public HttpResponse detailOrder(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = RequestUtil.getAllParameters(request);
        if (StringUtils.isBlank(params.get("order_no"))) {
            return httpResponse.failure(ResponseEnum.ILLEGAL_PARAMETER.getCode(), ResponseEnum.ILLEGAL_PARAMETER.getMessage());
        }
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_ORDER_DETAIL, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.RPC_NOT_FOUND.getCode(), ResponseEnum.RPC_NOT_FOUND.getMessage());
        } else if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.setData(Jackson.getBeanJsonData(serviceResponse.getResult(), OrderVo.class));
        } else {
            httpResponse.failure(ResponseEnum.RPC_EXCEPTION.getCode(), ResponseEnum.RPC_EXCEPTION.getMessage());
        }
        return httpResponse;
    }

    @PutMapping("cancel")
    public HttpResponse cancelOrder(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = RequestUtil.getAllParameters(request);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_ORDER_CANCEL, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.RPC_NOT_FOUND.getCode(), ResponseEnum.RPC_NOT_FOUND.getMessage());
        } else if (!Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.failure(ResponseEnum.RPC_EXCEPTION.getCode(), ResponseEnum.RPC_EXCEPTION.getMessage());
        }
        return httpResponse;
    }

}
